package com.zx.onlineeducation.service.ucenter.controller;


import com.zx.onlineeducation.common.util.JwtUtils;
import com.zx.onlineeducation.common.util.R;
import com.zx.onlineeducation.service.ucenter.entity.MemberDO;
import com.zx.onlineeducation.service.ucenter.entity.vo.LoginVO;
import com.zx.onlineeducation.service.ucenter.entity.vo.RegisterVO;
import com.zx.onlineeducation.service.ucenter.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员 前端控制器
 * </p>
 *
 * @author 郑雪
 * @since 2022-01-25
 */
@Api(tags = "会员管理")
@RestController
@RequestMapping("/ucenter/member")
@CrossOrigin // 解决跨域
public class MemberController {

    /**
     * 会员服务接口
     */
    @Autowired
    private MemberService memberService;

    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R login(
            @ApiParam(value = "会员登录视图对象")
            @RequestBody LoginVO loginVO) {
        String token = memberService.login(loginVO);
        return R.ok().data("token", token);
    }

    @ApiOperation("会员注册")
    @PostMapping("/register")
    public R register(
            @ApiParam(value = "会员注册视图对象")
            @RequestBody RegisterVO registerVO) {
        boolean result = memberService.register(registerVO);
        return result ? R.ok() : R.error();
    }

    @ApiOperation("获取当前登录的会员信息")
    @GetMapping("/info")
    public R info(HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        MemberDO memberDO = memberService.getById(id);
        return R.ok().data("memberInfo", memberDO);
    }

}

